{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "顺序链允许将多个链顺序连接起来，每个Chain的输出作为下一个Chain的输入，形成特定场景的流水线(Pipeline)\n",
    "顺序链有两种类型：\n",
    "    单个输入/输出：对应着SimpleSequentialChain\n",
    "    多个输入/输出：对应着SequentialChain\n",
    "SimpleSequentialChain\n",
    "    最简单的顺序链，多个链串联执行，每个步骤都有单一的输入和输出，一个步骤的输出就是下一个步骤的输入，无需手动映射\n",
    "\n",
    "SequentialChain\n",
    "    更通用的顺序链，具体来说：\n",
    "        多变量支持：允许不同子链有独立的输入/输出变量\n",
    "        灵活映射：需要显示定义变量如何从一个链传递到下一个链。即精准命名输入关键字和输出关键字，来明确链之间的关系\n",
    "        复杂流程控制：支持分支、条件逻辑(分别通过input_variables和ouput_variables配置输入和输出)"
   ],
   "id": "ea9af9724fb70b9e"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### SimpleSequentialChain使用",
   "id": "256cc10ca3f4f0f4"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain.chains.llm import LLMChain\n",
    "from langchain.chains.sequential import SimpleSequentialChain\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_openai import ChatOpenAI\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0.4)"
   ],
   "id": "ec9137b90702aacd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "chainA_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"你是一位精通各领域只是的知名教授\"),\n",
    "        (\"human\", \"请你尽可能详细解释一下,{knowledge}\")\n",
    "    ]\n",
    ")\n",
    "\n",
    "chainA_chains = LLMChain(llm=chat_model, prompt=chainA_template, verbose=True)"
   ],
   "id": "9805c73d280eb56",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "chainB_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"你非常善于提取文本中的重要信息，并做出简短的总结\"),\n",
    "        (\"human\", \"这是针对一个提问的完整的解释说明内容: {description}\"),\n",
    "        (\"human\", \"请根据上述说明，尽可能简短的输出重要的结论，请控制在20个字以内\")\n",
    "    ]\n",
    ")\n",
    "\n",
    "chainB_chains = LLMChain(llm=chat_model, prompt=chainB_template, verbose=True)"
   ],
   "id": "e036f52c7f39e680",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "full_chain = SimpleSequentialChain(chains=[chainA_chains, chainB_chains], verbose=True)\n",
    "# 针对于SimpleSequentialChain而言，唯一的输入变量名是：input\n",
    "response = full_chain.invoke(input={\"input\": \"什么是LangChain\"})\n",
    "print(response)"
   ],
   "id": "79aba14bf0d11886",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "##### SequentialChain使用",
   "id": "68968898e1b8c934"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "# 创建大模型实例\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\")"
   ],
   "id": "a8a491b422096d9d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain.chains.sequential import SequentialChain\n",
    "\n",
    "schainA_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"你是一位精通各领域的知名教授\"),\n",
    "        (\"human\", \"请你先尽可能详细的解释一下：{knowledge}，并且{action}\")\n",
    "    ]\n",
    ")\n",
    "\n",
    "schainA_chains = LLMChain(\n",
    "    llm=chat_model,\n",
    "    prompt=schainA_template,\n",
    "    verbose=True,\n",
    "    output_key=\"schainA_chains_key\")\n",
    "\n",
    "schainB_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"你非常善于提取文本中的重要信息，并做出简短的总结\"),\n",
    "        (\"human\", \"这是针对一个提问完整的解释说明内容：{schainA_chains_key}\"),\n",
    "        (\"human\", \"请你根据上述说明，尽可能简短的输出重要的结论，请控制在100个字以内\")\n",
    "    ]\n",
    ")\n",
    "\n",
    "schainB_chains = LLMChain(\n",
    "    llm=chat_model,\n",
    "    prompt=schainB_template,\n",
    "    verbose=True,\n",
    "    output_key=\"schainB_chains_key\")\n",
    "\n",
    "seq_chain = SequentialChain(chains=[schainA_chains, schainB_chains], input_variables=[\"knowledge\", \"action\"],\n",
    "                        output_variables=[\"schainA_chains_key\", \"schainB_chains_key\"], verbose=True)\n",
    "\n",
    "response = seq_chain.invoke({\"knowledge\": \"中国足球为什么踢得烂\", \"action\": \"举一个实际的例子\"})\n",
    "print(response)"
   ],
   "id": "e7c668f8b289732a",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
